#include "world.h"
#include <ctime>

World::World()
{
    aaa=210;
    int width=20;
    srand(time(NULL));

    m_graph=new Graph();
    m_graph->setBoundingRect(QRectF(0,0,0,0));

    m_sPath=new Path(m_graph);
    m_sPath->setBoundingRect(QRectF(0,0,0,0));

    m_mapelements.push_back(new MapElement(QPoint(100,40),QPoint(400,40)));
    m_mapelements.push_back(new MapElement(QPoint(100,40),QPoint(100,200)));
    m_mapelements.push_back(new MapElement(QPoint(400,40),QPoint(400,250)));
    m_mapelements.push_back(new MapElement(QPoint(330,250),QPoint(400,250)));
    m_mapelements.push_back(new MapElement(QPoint(400,350),QPoint(400,450)));
    m_mapelements.push_back(new MapElement(QPoint(300,350),QPoint(400,350)));
    m_mapelements.push_back(new MapElement(QPoint(480,0),QPoint(480,100)));
    m_mapelements.push_back(new MapElement(QPoint(480,100),QPoint(600,200)));
    m_mapelements.push_back(new MapElement(QPoint(700,200),QPoint(600,200)));
    m_mapelements.push_back(new MapElement(QPoint(40,450),QPoint(400,450)));
    m_mapelements.push_back(new MapElement(QPoint(40,450),QPoint(40,250)));




    int index=0;
    for(int i=0; i<30; ++i)
    {
        for(int j=0; j<40; ++j)
        {
            bool colide=false;
            foreach(MapElement* el, m_mapelements)
            {
                if(el->isColliding(QPoint(j*width,i*width)) ||
                        el->isColliding(QPoint(j*width,i*width+width)) ||
                        el->isColliding(QPoint(j*width+width,i*width)) ||
                        el->isColliding(QPoint(j*width+width,i*width+width))||
                        el->isColliding(QPoint(j*width-width,i*width))||
                         //el->isColliding(QPoint(j*width,i*width-width))||
                        el->isColliding(QPoint(j*width-width,i*width+width)))
                {
                    colide=true;
                }
            }


            if(!colide)
            {
                Node* node=m_graph->isExist(QPoint(j*width,i*width));

                if(node==NULL)
                {
                    node=new Node(index,QPoint(j*width,i*width));
                    m_graph->addNode(node);
                    ++index;

                }

                Node* tmp=m_graph->isExist(QPoint(j*width,i*width+width));
                if(tmp==NULL)
                {
                    tmp=new Node(index,QPoint(j*width,i*width+width));
                    m_graph->addNode(tmp);
                    ++index;

                }
                node->addEdge(new Edge(node,tmp));

                tmp=m_graph->isExist(QPoint(j*width+width,i*width));
                if(tmp==NULL)
                {
                    tmp=new Node(index,QPoint(j*width+width,i*width));
                    m_graph->addNode(tmp);
                    ++index;

                }
                node->addEdge(new Edge(node,tmp));

                tmp=m_graph->isExist(QPoint(j*width+width,i*width+width));
                if(tmp==NULL)
                {
                    tmp=new Node(index,QPoint(j*width+width,i*width+width));
                    m_graph->addNode(tmp);
                   ++index;

                }
                node->addEdge(new Edge(node,tmp,std::sqrt(2.0)));

                tmp=m_graph->isExist(QPoint(j*width-width,i*width+width));
                if(tmp==NULL)
                {
                    tmp=new Node(index,QPoint(j*width-width,i*width+width));
                    m_graph->addNode(tmp);
                    ++index;

                }
                node->addEdge(new Edge(node,tmp,std::sqrt(2.0)));

                tmp=m_graph->isExist(QPoint(j*width-width,i*width));
                if(tmp==NULL)
                {
                    tmp=new Node(index,QPoint(j*width-width,i*width));
                    m_graph->addNode(tmp);
                    ++index;

                }
                node->addEdge(new Edge(node,tmp));
            }
        }
    }

  /*  m_astarSearch=new AStar(m_graph,new ZeroHeuristic());
    m_astarSearch->setSource(1);

    m_astarSearch->setTarget(aaa);
    m_astarSearch->SearchPath();
    m_astarSearch->ConstructPath();

    m_sPath->newPath(m_astarSearch->newPath());*/



    //m_bot2->setBoundingRect(QRectF(0,0,5,5));





    m_bot=new Bot(this,m_sPath,m_graph);
    m_bot->setBoundingRect(QRectF(0,0,5,5));


    master=new ZombieMaster(this,new Path(m_graph),m_graph,m_bot);
    master->setBoundingRect(QRectF(0,0,5,5));

    m_bot->addMaster(master);

    for(int i=0; i<8; ++i)
    {
        zombies.push_back(new Zombie(this,new Path(m_graph),m_graph,master));
    }

    foreach(Zombie* b, zombies)
    {
        b->setBoundingRect(QRectF(0,0,5,5));
        addItem(b);
    }


    addItem(m_graph);
    addItem(m_bot);
    addItem(master);


    foreach(MapElement *el ,m_mapelements)
    {
        addItem(el);
    }

    addItem(m_sPath);
}

void World::mousePressEvent(QGraphicsSceneMouseEvent *event)
{

    //++aaa;



    //std::<< event->scenePos().x()<<" "<<event->scenePos().y()<<std::endl;
    //event->accept();

}

void World::collide()
{

    while(!bullets.empty())
    {
        addItem(bullets.dequeue());
    }

   // QList<QGraphicsItem *> deleteobj;

    foreach(QGraphicsItem *item, items())
    {
        Object *object = dynamic_cast<Object *> (item);
        if(object!=NULL)
        {
            object->step();

            foreach(MapElement *el, m_mapelements)
            {
                if(el->isColliding(object->scenePos()))
                {

                   removeItem(object);
                   //std::cout<<"dddddddddddddd" <<(object==NULL) <<std::endl;

                    //delete object;
                      //deleteobj.push_back(object);
                }
            }
        }
    }

        //for(int i=0; i<deleteobj.size(); ++i)
        //{
            //this->removeItem();
            //delete deleteobj[i];
        //}



/*
    m_bot->step();
    master->step();
    foreach(Zombie* b, zombies)
    {
        b->step();
    }

    foreach(MapElement *el, m_mapelements)
    {
        if(el->isColliding(m_bot->scenePos()))
        {
            m_bot->stop();
         }
    }*/

}
